<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Session
 *
 * @author charly
 */
class Session {
     private static $inst;
     private $lang = "fr";


    public static function getInstance() {
            if (!isset(self::$inst)) {
                $c = __CLASS__;
                self::$inst = new $c;
            }

            return self::$inst;
        }

    public function  __clone() {
    ;
    }


    public function  __construct() {

    }


    public function LogIn($pseudo, $psw) {
            $res = Database::select("Compte", "*", "( (pseudo=".S()->fetch()->quote($pseudo).") AND (password=".S()->fetch()->quote(sha1($psw)).") )");
            //@todo User::getInstance($id)->getLang()
            if (count($res)>1 || empty($res)) {
                return FALSE;
            } else {
                $_SESSION['auth']         = TRUE;
                $_SESSION['pseudo']       = $pseudo;
                $_SESSION['id']           = $res[0]['id'];
                $_SESSION['avatar']       = $res[0]['avatar'];
               return TRUE;
            }
    }

    public function sessionCheck() {
        if (!isset($_SESSION['auth'])) {
            if (isset($_SESSION['auth']) && ($_SESSION['auth'] != FALSE)) {
                session_regenerate_id(); // Securité, on change l'id de la session
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            return TRUE;
        }
    }

    public function sessionDestroy() {
        if (isset($_SESSION['auth'])) {
            unset($_SESSION);
            session_destroy();
        }
    }

    public function getLang() {
        return $this->lang;
    }

    /**
     * Création d'un joueur
     * @param string $pseudo
     * @param string $pass
     * @param string $pays
     * @return bool
     */
    public static function create($pseudo, $pass, $avatar, $email) {
        // @todo MAIL !
        $Pid = Database::insert("Compte", array('', $pseudo, $email, sha1($pass), $avatar, ''), 'ID');
        return $Pid;
    }
}
?>
